"""
@Author    : ghenyar
@Time      : 2025/8/28 12:31
@File      : admin_model
@Desc      : 后台管理员
"""
from datetime import datetime


from sqlalchemy import BigInteger, String, Boolean, DateTime, Integer, ForeignKey, Text, SmallInteger

from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.sql import func
from ..models import DatabaseModel, table_prefix



class AdminModel(DatabaseModel):
    __tablename__ = f"{table_prefix}admin"
    __table_args__ = {"comment": "管理员表"}

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True, comment="管理员ID")
    account: Mapped[str] = mapped_column(String(32), unique=True, nullable=False, comment="登录账号")
    password: Mapped[str] = mapped_column(String(255), nullable=False, comment="登录密码（已加密）")
    nickname: Mapped[str] = mapped_column(String(32), nullable=False, comment="昵称")
    phone: Mapped[str] = mapped_column(String(11), nullable=True, comment="手机号")
    email: Mapped[str] = mapped_column(String(100), nullable=True, comment="电子邮箱")
    avatar: Mapped[str] = mapped_column(String(255), nullable=True, comment="头像 URL")

    is_super: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, comment="是否超级管理员")
    is_locked: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, comment="是否锁定")

    last_login: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="最后登录时间")
    last_ip: Mapped[str | None] = mapped_column(String(45), nullable=True, comment="登录 IP")

    types: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment="管理员类型 0-后台添加")
    accept_ms: Mapped[str | None] = mapped_column(String(255), nullable=True,
                                                  comment="可接受消息类型，逗号分隔，1-订单 2-提现 …")

    created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.now(),
                                                 comment="创建时间")
    updated_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, onupdate=func.now(),
                                                        comment="更新时间")


class AdminRoleModel(DatabaseModel):
    __tablename__ = f"{table_prefix}admin_role"
    __table_args__ = {"comment": "管理员-角色关联表"}

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True, comment="管理员角色ID")
    admin_id: Mapped[int] = mapped_column(BigInteger, ForeignKey(f"{table_prefix}admin.id", ondelete="CASCADE"),
                                          nullable=False,
                                          comment="管理员ID")
    role_id: Mapped[int] = mapped_column(BigInteger, nullable=False,comment="角色ID")




class AdminLogModel(DatabaseModel):
    __tablename__ = f"{table_prefix}admin_log"
    __table_args__ = {"comment": "管理员操作日志表"}

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True, comment="操作日志ID")
    admin_id: Mapped[int] = mapped_column(BigInteger, default=0, nullable=True, comment="操作人ID")
    account: Mapped[str] = mapped_column(String(32), nullable=False, comment="操作人账号")

    method: Mapped[str] = mapped_column(String(7), nullable=False, comment="HTTP 方法")
    uri: Mapped[str] = mapped_column(String(255), nullable=False, comment="请求路径")
    req_param: Mapped[str] = mapped_column(Text, nullable=True, comment="请求参数(JSON)")

    # 响应信息
    status_code: Mapped[int] = mapped_column(SmallInteger, nullable=False, comment="HTTP 状态码")
    result: Mapped[str] = mapped_column(Text, nullable=True, comment="响应体(JSON)")
    result_msg: Mapped[str] = mapped_column(String(500), nullable=True, comment="业务提示消息")

    ip: Mapped[str] = mapped_column(String(45), nullable=True, comment="操作IP")  # IPv6 最长 45
    web_browser: Mapped[str] = mapped_column(String(255), nullable=True, comment="浏览器 UA")
    os_name: Mapped[str] = mapped_column(String(100), nullable=True, comment="操作系统")
    desc_message: Mapped[str] = mapped_column(Text, nullable=True, comment="操作内容描述")
    types: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=100,
                                       comment="日志类型 0-登录 1-查询 2-新增 3-修改 4-删除 5-上传 100-其他")
    created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.now(),
                                                 comment="创建时间")
